﻿using Airthmetic;
using BaseTool;
using OpenCvSharp.Extensions;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace StructuredLightSystemUI
{
    /// <summary>
    /// 图像列表窗体
    /// </summary>
    public partial class ImageListForm : InnerForm
    {
        /// <summary> 显示列数 </summary>
        private int ControlColCount = 0;
        /// <summary> 图像宽度 </summary>
        private int ControlWidth = 100;

        /// <summary> 光栅列表 </summary>
        private List<PatternData> PatternList;
        /// <summary> 刷新图像列表控件 </summary>
        public static Action RefreshImageAction;

        #region 构造、初始化
        /// <summary>
        /// 构造
        /// </summary>
        public ImageListForm()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 构造（条纹光栅）
        /// </summary>
        public ImageListForm(List<PatternData> PatternList)
        {
            InitializeComponent();

            this.PatternList = PatternList;
        }
        //加载前
        private void ImageListForm_Load(object sender, EventArgs e)
        {
            RefreshImageAction += RefreshImageControl;
        }
        //显示后
        private void ImageListForm_Shown(object sender, EventArgs e)
        {
            RefreshImageControl();
        }
        //正在关闭
        private void ImageListForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            RefreshImageAction -= RefreshImageControl;
        }
        //已经关闭
        private void ImageListForm_FormClosed(object sender, FormClosedEventArgs e)
        {

        }
        #endregion

        /// <summary>
        /// 刷新控件
        /// </summary>
        private void RefreshImageControl()
        {
            Action run = () =>
            {
                try
                {
                    int edgeSize = 3;
                    int colCount = ControlColCount;
                    if (colCount == 0)
                        colCount = (int)Math.Floor(1.0 * panel1.Size.Width / (ControlWidth + edgeSize));
                    colCount = colCount <= 0 ? 1 : colCount;

                    panel1.Controls.Clear();
                    if (PatternList == null || PatternList.Count == 0)
                        return;
                    for (int i = 0; i < PatternList.Count; i++)
                    {
                        int colIndex = colCount == 1 ? 0 : i % colCount;
                        int rolIndex = colCount == 1 ? i : (int)Math.Floor(1.0 * i / colCount);
                        ConFixImage fixImage = new ConFixImage();
                        fixImage.SetSize(ControlWidth);
                        fixImage.Dock = DockStyle.None;
                        fixImage.Location = new Point(
                            edgeSize + colIndex * (fixImage.Width + edgeSize),
                            edgeSize + rolIndex * (fixImage.Height + edgeSize));
                        panel1.Controls.Add(fixImage);

                        fixImage.ShowImage = PatternList[i].GetFullImage.ToBitmap();
                        fixImage.Text = $"[{(i + 1).ToString().PadLeft(2, '0')}] {PatternList[i].ImageWidth}x{PatternList[i].ImageHeight}";
                    }
                }
                catch (Exception ex)
                {
                    Log.SaveError(ex);
                }
            };
            if (this.InvokeRequired)
                this.Invoke(run);
            else
                run();
        }
        //显示列数
        private void tstb_ColCount_TextChanged(object sender, EventArgs e)
        {
            int value;
            if (int.TryParse(tstb_ColCount.Text, out value) &&
                value >= 0)
            {
                ControlColCount = value;
                RefreshImageControl();
            }
            else
            {
                tstb_ColCount.Text = ControlColCount.ToString();
            }
        }
        //图像宽度
        private void tstb_ColWidth_TextChanged(object sender, EventArgs e)
        {
            int value;
            if (int.TryParse(tstb_ColWidth.Text, out value) &&
                value >= ConFixImage.ImageWidthMin)
            {
                ControlWidth = value;
                RefreshImageControl();
            }
            else
            {
                tstb_ColCount.Text = ControlWidth.ToString();
            }
        }

        //刷新
        private void tsb_refresh_Click(object sender, EventArgs e)
        {
            RefreshImageControl();
        }
    }
}
